/*
 * @Author: your name
 * @Date: 2020-06-24 11:57:21
 * @LastEditTime: 2020-07-06 14:24:31
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \jinzheng\vue.config.js
 */
const path = require('path')

const resolve = dir => {
  return path.join(__dirname, dir)
}

// 访问路径相对目录
const distDirUrl = ''

const version = new Date().getTime()

const hiddenSourceMap = [
  'alpha',
  'aliPro',
  'pro'
].includes(process.env.NODE_ENV)

const BASE_URL = process.env.NODE_ENV === 'production'
  ? '/'
  : '/'
// const dist = `dist-${process.env.NODE_ENV}` || 'dist'
const dist = 'dist'
// const dist = process.env.NODE_ENV === 'production' ? 'dist' : 'dist'
module.exports = {
  publicPath: BASE_URL,
  outputDir: dist,
  assetsDir: `${distDirUrl}static`,
  // 生成的静态资源在它们的文件名中包含了 hash 以便更好的控制缓存
  filenameHashing: true,
  // 如果你不需要使用eslint，把lintOnSave设为false即可
  lintOnSave: true,

  chainWebpack: config => {

    // 版本信息
    config
      .plugin('html')
      .tap(args => {
        Object.assign(args[0], {
          buildInfo: {
            version,
            versionEnv: process.env.NODE_ENV
          }
        })
        return args
      })
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
      .set('_c', resolve('src/components'))
    config
      .output
      .filename(`${distDirUrl}js/[name].[hash:3]${version}.js`)
      .end()
    config.module
      .rule('images')
      .test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
      .use('url-loader')
      .tap(options => {
        options.limit = 10000
        options.fallback = {
          loader: 'file-loader',
          options: {
            name: `${distDirUrl}static/img/[name].${version}.[ext]`
          }
        }
        return options
      })
    config.resolve.symlinks(true)
    config.module
      .rule('svg')
      .test(/\.(svg)(\?.*)?$/)
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: `${distDirUrl}static/img/[name].${version}.[ext]`
      })
    config.module
      .rule('fonts')
      .test(/\.(woff2?|eot|ttf|otf)(\?.*)?$/)
      .use('url-loader')
      .loader('url-loader')
      .options({
        limit: 10000,
        fallback: {
          loader: 'file-loader',
          options: {
            name: `${distDirUrl}static/fonts/[name].${version}.[ext]`
          }
        }
      })

    hiddenSourceMap && config.devtool("hidden-source-map")
  },
  // 设为false打包时不生成.map文件
  productionSourceMap: false,
  // 构建时开启多进程处理babel编译
  parallel: require('os').cpus().length > 1,
  // 这里写你调用接口的基础路径，来解决跨域，如果设置了代理，那你本地开发环境的axios的baseUrl要写为 '' ，即空字符串
  devServer: {
    // hot: true,
    // port: 8089,
    proxy: {
      '^/api': {
        target: 'http://127.0.0.1:9000/api',
        changeOrigin: true,
        // ws: true,
        pathRewrite: {
          '^/api': ''
        }
      },
      // '^/wui': {//泛微测试代理
      //   target: 'http://10.0.100.247:8090/',
      //   changeOrigin: true,
      //   // ws: true,
      //   pathRewrite: {
      //     // '^/myapk': ''
      //   }
      // },
      '^/wui': {//泛微正式代理
        target: 'http://oa.jinzhenghb.com:8886/',
        changeOrigin: true,
        // ws: true,
        pathRewrite: {
          // '^/myapk': ''
        },
        '/websocket': {
          target: 'http://127.0.0.1:9000/websocket',　　//target为目标变量
          ws: true,
          pathRewrite: {
            '^/websocket': ''
          }
        }
      },
    }
  },
  configureWebpack:{
    module: {
        rules: [
            {
                test: /.mjs$/,
                include: /node_modules/,
                type: "javascript/auto"
            },
        ]
    }
}
    
}
